<!--
	作者：lsd
	时间：2020-4-27
	描述：物料组弹窗
-->
<template>
  <el-dialog
    title="选择物料组"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    width="50%"
    v-dialogDrag
    append-to-body
    custom-class="el-dialog-materials"
    :close-on-click-modal="false"
    :close-on-press-escape="false">

    <el-form ref="form" label-width="40px">
      <el-row :gutter="10" class="m-condition">
        <el-col :span="9">
          <el-form-item label="编码">
            <el-input
              value-key="id"
              v-model.trim="formSearch.matgroupNo"
              placeholder="请输入编码"
              clearable
              @input="search(true)"
              maxlength="20">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label="名称" class="rightItem">
            <el-input
              value-key="id"
              v-model.trim="formSearch.matgroupName"
              placeholder="请输入名称"
              @input="search(true)"
              clearable
              maxlength="20"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="3" :offset="3">
          <el-button type="primary" @click="selectedSupplier">确定</el-button>
        </el-col>
      </el-row>
    </el-form>
    <div class="m-agTableModel">
      <ag-grid-vue
        ref="agGridComponent"
        class="m-agTableModel ag-theme-balham"
        :gridOptions="gridOptions"
        :columnDefs="columnDefs"
        rowSelection="single"
        v-loading="loading"
      ></ag-grid-vue>
    </div>
    <div class="m-pageModel m-pBottom">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.currentPage"
        :page-sizes="[30, 50, 100, 150, 200]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </el-dialog>
</template>
<script>
import _Api from "@/api";
import localeText from "@/i18n/agGridText";
import agGridComponents from "@/components/agGridComponents.js";

/**
 * @group dialogSrmMaterials
 * 物料组对话框
 */
export default {
  name: 'dialogSrmMaterials',
	components:{
		agGridComponents,
  },
  props:{
    //显示弹框
    dialogVisible: {
      //类型布尔值
      type: Boolean,
      //默认值是true
      default: true
    }
  },
  data(){
    return{
      formSearch:{//查询表
				matgroupName: '',
				matgroupNo: '',
			},
			optionsCompanyName: [],
			rowData: [], // 表格数据
			//表格参数
			gridOptions: '',
			columnDefs: [],
			//页脚参数
			pageInfo: {
				pageSize: 30,
				currentPage: 1,
			},
			total: 0,

			//加载参数
			loading: false,//页面的
    }
  },
  beforeMount () {
		this.gridOptions = {
		context: {
        componentParent: this
      },
      rowSelection: "single",
      enableColResize: true,
      localeText: localeText,
      onRowDoubleClicked: this.rowDoubleClicked
		};
		this.columnDefs = [
			{	headerName: "编码", field: "matgroupNo", width: 200, checkboxSelection: true },
			{ headerName: "名称", field: "matgroupName", width: 200 },
		];

	},
	mounted(){
		this.search();
	},
  methods:{
    /**
     * @vuese
     * 查询方法
     * 
     *  @arg isClick:参数是布尔值， true就是点击触发，false就是非点击触发
     */
    search(isClick){
      this.loading = true;
      this.pageInfo.currentPage = isClick ? 1 : this.pageInfo.currentPage;
      let param = {
        ...this.formSearch,
        ...this.pageInfo
      };
      _Api
        .searchMaterials(param)
        .then(res => {
          this.loading = false;
          if (res.data.code == 1) {
            this.rowData = res.data.rows;
            this.total = res.data.total
            this.$refs.agGridComponent.gridOptions.api.setRowData(this.rowData);
          } else {
            this.$alert(res.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
          }
        })
        .catch(err => {
          this.loading = false;
          this.$alert(err.data.info, { showClose: false, confirmButton: "确定", customClass: "alertFailure" });
        });
    },
    
    /**
     * @vuese
     * 改变页脚总条数方法
     */
    handleSizeChange (val) {
      this.pageInfo.pageSize = val;
      this.pageInfo.currentPage = 1;
      this.search();
    },

    /**
     * @vuese
     * 翻页方法
     */
    handleCurrentChange (val) {
      this.pageInfo.currentPage = val;
      this.search();
    },

    /**
     * @vuese
     * 关闭对话框方法
     */
    handleClose() {
      this.$emit("dialogClose", false);
    },

    /**
     * @vuese
     * 确认选择行数据
     */
    selectedSupplier() {
      let row = this.gridOptions.api.getSelectedRows();
      if (row.length <= 0) {
        this.$alert("提示：请选择数据", {
          showClose: false,
          confirmButtonText: "确定",
          customClass: "alertPrompt"
        });
        return;
      }
      this.$emit("setSelectData", row[0]);
      this.handleClose();
    },

    /**
     * @vuese
     * 双击行回调方法
     */
    rowDoubleClicked() {
      this.selectedSupplier();
    }
  }
}
</script>
<style lang="less">
.el-dialog-materials{
  min-width: 720px;
}
</style>